package br.com.intelsys.basics.spring.validator;

import org.apache.log4j.Logger;

public abstract class IValidator {

	private static final Logger log = Logger.getLogger(IValidator.class);

	protected IValidatorException addError(IValidatorException iValidatorException, String error, String... param) {
		if (iValidatorException == null) {
			return new IValidatorException(error, param);
		}
		return iValidatorException.addError(error, param);
	}

	protected void checkIValidatorException(IValidatorException iValidatorException) throws IValidatorException {
		checkIValidatorException(iValidatorException, null);
	}

	protected void checkIValidatorException(IValidatorException iValidatorException, Object entity) throws IValidatorException {
		if (iValidatorException != null && iValidatorException.hasError()) {
			iValidatorException.setEntity(entity);
			throw iValidatorException;
		}
	}

	public void addUnknowError() throws IValidatorException {
		addUnknowError(null);
	}

	public void addUnknowError(Throwable e) throws IValidatorException {
		if (e != null) {
			log.error("Unknow error from IValidator: ", e);
		}
	}

	public void throwError(String error) throws IValidatorException {
		throw new IValidatorException(error);
	}

	protected IValidatorException getIValidatorException(Throwable e) {
		return new IValidatorException(e);
	}

	protected IValidatorException getIValidatorException() {
		return new IValidatorException();
	}

}
